// ignore_for_file: non_constant_identifier_names

import 'dart:convert';

///
/// deepseek对话模型的 ID
///
enum XChatModel {
  // 对话补全模型
  chat('deepseek-chat'),
  // 推理模型
  reasoner('deepseek-reasoner');

  const XChatModel(this.type);

  final String type;
}

///
/// deepseek chat response 模型
///
class XChatResponse {
  final String? id;
  final String? object;
  final int? created;
  final String? model;
  final String? system_fingerprint;
  final List<XChatChoices> choices;
  final XChatUsage? usage;

  XChatResponse({
    this.id,
    this.object,
    this.created,
    this.model,
    this.system_fingerprint,
    this.choices = const [],
    this.usage,
  });

  factory XChatResponse.fromJson(Map<String, dynamic> json) => XChatResponse(
        id: json['id'],
        object: json['object'],
        created: json['created'],
        model: json['model'],
        system_fingerprint: json['system_fingerprint'],
        choices: json['choices'] == null ? [] : (json['choices'] as List).map((e) => XChatChoices.fromJson(e)).toList(),
        usage: json['usage'] == null ? null : XChatUsage.fromJson(json['usage']),
      );
}

class XChatChoices {
  final int? index;
  final dynamic logprobs;
  final String? finish_reason;
  final XChatChoicesMessage? message;

  XChatChoices({
    this.index,
    this.logprobs,
    this.finish_reason,
    this.message,
  });

  factory XChatChoices.fromJson(Map<String, dynamic> json) => XChatChoices(
        index: json['index'],
        logprobs: json['logprobs'],
        finish_reason: json['finish_reason'],
        message: json['message'] == null ? null : XChatChoicesMessage.fromJson(json['message']),
      );
}

class XChatChoicesMessage {
  final String? role;
  final String? content;

  XChatChoicesMessage({
    this.role,
    this.content,
  });

  factory XChatChoicesMessage.fromJson(Map<String, dynamic> json) => XChatChoicesMessage(
        role: json['role'],
        content: json['content'],
      );
}

class XChatUsage {
  int? prompt_tokens;
  int? completion_tokens;
  int? total_tokens;
  XChatUsageDetails? prompt_tokens_details;
  int? prompt_cache_hit_tokens;
  int? prompt_cache_miss_tokens;

  XChatUsage({
    this.prompt_tokens,
    this.completion_tokens,
    this.total_tokens,
    this.prompt_tokens_details,
    this.prompt_cache_hit_tokens,
    this.prompt_cache_miss_tokens,
  });

  factory XChatUsage.fromJson(Map<String, dynamic> json) => XChatUsage(
        prompt_tokens: json['prompt_tokens'],
        completion_tokens: json['completion_tokens'],
        total_tokens: json['total_tokens'],
        prompt_cache_hit_tokens: json['prompt_cache_hit_tokens'],
        prompt_cache_miss_tokens: json['prompt_cache_miss_tokens'],
        prompt_tokens_details: json['prompt_tokens_details'] == null ? null : XChatUsageDetails.fromJson(json['prompt_tokens_details']),
      );
}

class XChatUsageDetails {
  int? cached_tokens;

  XChatUsageDetails({
    this.cached_tokens,
  });

  factory XChatUsageDetails.fromJson(Map<String, dynamic> json) => XChatUsageDetails(
        cached_tokens: json['cached_tokens'],
      );
}

///
/// Represents a DeepSeek API error
///
class XException implements Exception {
// {
//   "error": {
//     "message": "Authentication Fails (no such user)",
//     "type": "authentication_error",
//     "param": null,
//     "code": "invalid_request_error"
//   }
// }

  /// The error message
  final String message;

  /// The error status code
  final int? statusCode;

  /// The error type
  final String? type;

  /// The error parameter
  final String? param;

  /// The error code
  final String? code;

  XException(
    this.message, {
    this.statusCode,
    this.type,
    this.param,
    this.code,
  });

  XException._fromMap(Map<String, dynamic> map, {this.statusCode})
      : message = map['message'] ?? 'Unknown error',
        type = map['type'],
        param = map['param'],
        code = map['code'];

  XException.fromBody(String body, int statusCode) : this._fromMap(_parseBody(body), statusCode: statusCode);

  static Map<String, dynamic> _parseBody(String body) {
    try {
      final json = jsonDecode(body);
      return json['error'] ?? {};
    } catch (e) {
      return {'message': 'Failed to parse error response: $body'};
    }
  }

  @override
  String toString() => 'XException(message: $message, statusCode: $statusCode, type: $type, param: $param, code: $code)';
}
